package com.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.entity.User;
import com.example.demo.exception.BusinessException;
import com.example.demo.mapper.UserMapper;
import com.example.demo.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.utils.HttpCode;
import com.example.demo.utils.StringUtils;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author mybatis-plus-generator
 * @since 2022-06-25
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Override
    public Page<User> pageList(User param) {
        if (StringUtils.isEmpty(param.getCurrent()) || StringUtils.isEmpty(param.getSize())) {
            throw new BusinessException(HttpCode.BAD_REQUEST, "分页参数为空");
        }
        String keyword = param.getKeyword();

        QueryWrapper<User> qw = new QueryWrapper<>();
        if(StringUtils.isNotEmpty(keyword)){
            String searchKey = "%" + keyword + "%";
            qw.and(wrapper -> wrapper.like("id", searchKey)
                    .or().like("username", searchKey)
                    .or().like("email", searchKey)
                    .or().like("sex",searchKey));
        }
        Page<User> pageConfig = new Page<>(param.getCurrent(), param.getSize());
        Page<User> pageRecords = super.page(pageConfig, qw);
        return pageRecords;
    }
}
